
library("Matrix")
library('foreign')

################################################
#### build peer demographic and specific crime variables
###############################################


#load data
Demo=read.csv("ResPeerDemo_ReSTAT.csv")
ones=rep(1,length(Demo$numzz))
totday=peermatrix%*%ones

# make the data frame
peerdemos=data.frame(numzz=Demo$numzz)

x=as.matrix(peermatrix %*% Demo$black / totday)
peerdemos$pblack=x
x=as.matrix(peermatrix %*% Demo$hisp / totday)
peerdemos$phisp=x
x=as.matrix(peermatrix %*% Demo$age / totday)
peerdemos$page=x
x=as.matrix(peermatrix %*% Demo$white / totday)
peerdemos$pwhite=x

write.dta(peerdemos, "ResPeerDemos_ReSTAT.dta")

##### criminal skill transfer
skills=read.csv('skilltrans_ReSTAT.csv')
skills[is.na(skills)]=0

skillsdf=data.frame(numzz=skills$numzz)
skillsdf$psex_off=as.vector(peermatrix %*% skills$sex_off / totday)
skillsdf$pagg_ass=as.vector(peermatrix %*% skills$agg_ass / totday)
skillsdf$pass_nonag=as.vector(peermatrix %*% skills$ass_nonag / totday)
skillsdf$probbery=as.vector(peermatrix %*% skills$robbery / totday)
skillsdf$pweapon=as.vector(peermatrix %*% skills$weapon / totday)
skillsdf$pdrug_off=as.vector(peermatrix %*% skills$drug_off / totday)
skillsdf$pmisdrug_off=as.vector(peermatrix %*% skills$misdrug_off / totday)
skillsdf$pburglary=as.vector(peermatrix %*% skills$burglary / totday)
skillsdf$pauto_theft=as.vector(peermatrix %*% skills$auto_theft / totday)
skillsdf$ppetlarceny=as.vector(peermatrix %*% skills$petlarceny / totday)
skillsdf$pgralarceny=as.vector(peermatrix %*% skills$gralarceny / totday)
skillsdf$plarceny=as.vector(peermatrix %*% skills$larceny / totday)
skillsdf$passault=as.vector(peermatrix %*% skills$assault / totday)
skillsdf$pdrugtot=as.vector(peermatrix %*% skills$drugtot / totday)


write.dta(skillsdf, "skills_ReSTAT.dta")

###################################################
########### build peer risk, ch, factors, risk levels
###################################################

#load data
Res=read.csv("ResPeerMain_ReSTAT.csv")

# if risk score is missing, replace overlap with zero in peermatrix
peermatrixsh=peermatrix
peermatrixsh[,is.na(Res$rskscr)]=0
totdaysh=peermatrixsh%*%ones
save(list=c("peermatrixsh", "totdaysh"),file="peermatrixsh_ReSTAT.RData")

# replace missing values with zeros
Res$chscr[is.na(Res$chscr)]=0
Res$rskscr[is.na(Res$rskscr)]=0

# make the data frame
peermeasures=data.frame(numzz=Res$numzz)

#Create CH and Risk peer measures
peermeasures$peerch=as.matrix(peermatrixsh %*% Res$chscr / totdaysh)
peermeasures$peerrsk=as.matrix(peermatrixsh %*% Res$rskscr / totdaysh)

# Create peer risk level variables
x=as.matrix(peermatrixsh %*% Res$rsklev_1 / totdaysh)
peermeasures$prsklev_1=x
x=as.matrix(peermatrixsh %*% Res$rsklev_2 / totdaysh)
peermeasures$prsklev_2=x
x=as.matrix(peermatrixsh %*% Res$rsklev_3 / totdaysh)
peermeasures$prsklev_3=x
x=as.matrix(peermatrixsh %*% Res$rsklev_4 / totdaysh)
peermeasures$prsklev_4=x
x=as.matrix(peermatrixsh %*% Res$rsklev_5 / totdaysh)
peermeasures$prsklev_5=x
x=as.matrix(peermatrixsh %*% Res$rsklev_6 / totdaysh)
peermeasures$prsklev_6=x
x=as.matrix(peermatrixsh %*% Res$rsklev_7 / totdaysh)
peermeasures$prsklev_7=x
x=as.matrix(peermatrixsh %*% Res$rsklev_8 / totdaysh)
peermeasures$prsklev_8=x
x=as.matrix(peermatrixsh %*% Res$rsklev_9 / totdaysh)
peermeasures$prsklev_9=x

# homemade peer factors
x=as.matrix(peermatrixsh %*% Res$h_drugalc / totdaysh)
peermeasures$ph_drugsalc=x
x=as.matrix(peermatrixsh %*% Res$h_schoolprob / totdaysh)
peermeasures$ph_schoolprob=x
x=as.matrix(peermatrixsh %*% Res$h_famprob / totdaysh)
peermeasures$ph_famprob=x
x=as.matrix(peermatrixsh %*% Res$h_trauma / totdaysh)
peermeasures$ph_trauma=x
x=as.matrix(peermatrixsh %*% Res$gangz / totdaysh)
peermeasures$ph_gangz=x


write.dta(peermeasures, file="PeerMain_ReSTAT.dta")

#############################################
####### build peer noncogs
#############################################

#build the peer noncog measures
noncog=read.csv("ResPeerNoncog_ReSTAT.csv")
peernoncog=data.frame(numzz=noncog$numzz)

#new peer matrices for noncog (full screen) results
peermatrix_noncog=peermatrixsh
peermatrix_noncog[,is.na(noncog$skills_r)]=0
ones=rep(1,35536)
totday_noncog=peermatrix_noncog%*%ones

# clean up missing values
noncog[is.na(noncog)]=0


# build the measures
x=as.matrix(peermatrix_noncog %*% noncog$skills_p / totday_noncog)
peernoncog$pskills_p=x
x=as.matrix(peermatrix_noncog %*% noncog$agg_p / totday_noncog)
peernoncog$pagg_p=x
x=as.matrix(peermatrix_noncog %*% noncog$att_p / totday_noncog)
peernoncog$patt_p=x
x=as.matrix(peermatrix_noncog %*% noncog$skills_r / totday_noncog)
peernoncog$pskills_r=x
x=as.matrix(peermatrix_noncog %*% noncog$agg_r / totday_noncog)
peernoncog$pagg_r=x
x=as.matrix(peermatrix_noncog %*% noncog$att_r / totday_noncog)
peernoncog$patt_r=x
x=as.matrix(peermatrix_noncog %*% noncog$noncog_r / totday_noncog)
peernoncog$pnoncog_r=x
x=as.matrix(peermatrix_noncog %*% noncog$noncog_p / totday_noncog)
peernoncog$pnoncog_p=x

# build detailed peer noncog measures
peernoncog$pattitude=as.matrix(peermatrix_noncog %*% noncog$attitude / totday_noncog)
peernoncog$pagg=as.matrix(peermatrix_noncog %*% noncog$agg / totday_noncog)
peernoncog$pimplsv=as.matrix(peermatrix_noncog %*% noncog$implsv / totday_noncog)
peernoncog$psocskill=as.matrix(peermatrix_noncog %*% noncog$socskill / totday_noncog)
peernoncog$pfuture=as.matrix(peermatrix_noncog %*% noncog$future / totday_noncog)


write.dta(peernoncog, file="peernoncog_ReSTAT.dta")


